package com.itheima.controller.backend;

import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.Result;
import com.itheima.pojo.CheckItem;
import com.itheima.pojo.dto.CheckPageDto;
import com.itheima.service.CheckItemService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author User
 */
@Slf4j
@RestController
@RequestMapping("/checkitem")
@Api(tags ="检查项相关接口")
public class CheckItemController {
    @Autowired
    private CheckItemService checkItemService;

    /**
     * 新增检查项
     * @return
     */
    @PostMapping("/add")
    @ApiOperation("新增检查项")
    public Result add(@RequestBody CheckItem checkItem){
        checkItemService.add(checkItem);
        return new Result(true,"新增检查项成功");
    }

    @GetMapping("/delete")
    public Result delete(@RequestParam Integer id){
        checkItemService.delete(id);
        return new Result(true,"删除检查项成功");
    }

    /**
     * 根据检查组id查询检查项id
     * @param id
     * @return
     */
    @GetMapping("/findCheckItemIdsByCheckGroupId")
    @ApiOperation("根据检查组id查询检查项id")
    public Result findCheckItemIdsByCheckGroupId(@RequestParam Integer id){
        log.info("根据检查组id查询检查项ids：{}",id);
        List<Integer> checkItemIds= checkItemService.findCheckItemIdsByCheckGroupId(id);
        return new Result(true,"查询成功",checkItemIds);
    }

    @PostMapping("/edit")
    @ApiOperation("编辑检查项")
    public Result edit(@RequestBody CheckItem checkItem){
        log.info("编辑检查项：{}",checkItem);
        checkItemService.edit(checkItem);
        return new Result(true,"编辑检查项成功");
    }

    /**
     * 分页查询检查项
     * @param checkPageDto
     * @return
     */
    @PostMapping("/findPage")
    @ApiOperation("分页查询检查项")
    public PageResult findPage(@RequestBody CheckPageDto checkPageDto){
        log.info("分页查询检查项：{}", checkPageDto);
        PageResult pageResult = checkItemService.findPage(checkPageDto);
        return pageResult;

    }

    /**
     * 查询所有检查项
     * @return
     */
    @GetMapping("/findAll")
    @ApiOperation("查询所有检查项")
    public Result findAll(){
        log.info("查询所有检查项");
        List<CheckItem> checkItems = checkItemService.findAll();
        return new Result(true,"查询成功",checkItems);
    }

    @GetMapping("/findById")
    @ApiOperation("根据检查项id查询检查项")
    public Result findAllById(String id){
        log.info("根据检查项id查询检查项：{}",id);
        CheckItem checkItem = checkItemService.findById(id);
        return new Result(true,"查询检查项成功",checkItem);
    }
}
